#pragma once

//Win Main Library//
#pragma comment(lib, "winmm.lib")

//Required Direct3D 9 Headers and Libraries//
#include<d3d9.h>
#include<d3dx9.h>
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

//dinput//
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")

#include<cmath>

#include <stdlib.h>
#include <time.h>

#include"Enums.h"
#include"Definitions.h"
#include"Menu.h"
#include"Board.h"
#include"CXBOXController.h"
#include"Player.h"
#include"Hand.h"
#include"Deck.h"
#include"Camera.h"
#include"PlayerController.h"
#include"Poster.h"
#include"SoundEngine.h"



struct CUSTOMVERTEX {FLOAT X, Y, Z; FLOAT U, V;};
//Define Class//
class CDirectXFramework
{

	//Variables//

private:
	//Application Variables----------------------------------------//
	HWND				m_hWnd;			//Window Handle//
	bool				m_bVsync;		//Vertical Sync Boolean//
	bool				m_bKeyDown[256];// Check if key was down last frame

	//Direct3D Variables-------------------------------------------//
	IDirect3D9*			m_pD3DObject;	//Direct3D 9 Object Pointer//
	IDirect3DDevice9*	m_pD3DDevice;	//Direct3D 9 Device Pointer//
	D3DCAPS9			m_D3DCaps;		//Device Capabilites//

	//Camera Variables---------------------------------------------//
	CCamera				m_Camera;		//The Camera//

	//Direct Input Variables---------------------------------------//
	IDirectInput8*		m_pDIObject;	//DirectInput Object//
	IDirectInputDevice8* m_pDIKeyboard;	//Keyboard Device//
	IDirectInputDevice8* m_pDIMouse;	//Mouse Device//
	DIMOUSESTATE2		m_MouseState;	//Mouse's State//
	POINT				m_MousePosition;//Mouse's Position//
	TCHAR				m_tcMousePosition[32];//Print Mouse Position//


	//Font Variables-----------------------------------------------//
	int					m_NumFonts;		//Used in Error Checking//
	ID3DXFont*			m_pD3DFont;		//Font Object Pointer//
	ID3DXFont*			m_pD3DFontBig;	//same but bigger

	//Sprite Variables---------------------------------------------//
	ID3DXSprite*		m_pD3DSprite;	//Sprite Object Pointer//
	IDirect3DTexture9*	m_TempTex;		//Used to Hold Texture Briefly//
	D3DXIMAGE_INFO		m_TempInfo;

	LPDIRECT3DTEXTURE9 texture;

	//Frames Per Second Variables----------------------------------//
	DWORD				m_dwFrames;		//Number of Frames//
	DWORD				m_dwCurrentTime;
	DWORD				m_dwLastUpdateTime;
	DWORD				m_dwElapsedTime;
	TCHAR				m_tcFPS[32];	//String to Display//

	//Matrices-----------------------------------------------------//
	D3DXMATRIX m_matTrans, m_matRot, m_matScale, m_matWorld;
	D3DXMATRIX m_matView;		//the view transform matrix//
	D3DXMATRIX m_matProj;		//the projection transform matrix//

	D3DXMATRIX m_matRotateY;

	//State Machines-----------------------------------------------//
	GameState m_GameState;	//State the Game is currently in//
	PlayState m_PlayState;	//Used to control the game update//
	MoveState m_MoveState;
	PickState m_PickState;	//Determines if we are picking this render update or not//
	PickThis m_PickThis;	//Determines what will be picking//
	MapState  m_MapState;	//Moves the camera to the map state or not//

	//Error Checking-----------------------------------------------//
	HRESULT checkpoint;			//Used in Various Error Checking//
	TCHAR	m_tcDebug[32];		//String to Display//
	int		m_ClickOnce;		//Mouse Variable for Clicking//
	TCHAR	m_tcKB[32];			//Show which keys are pressed//

	//Menu Variables-----------------------------------------------//
	CMenu	m_Menu;
	int m_index;
	D3DCOLOR m_menuFontColor;
	D3DXCOLOR m_range[NumberOfButtons];
	int m_TitleColorIndex;

	//Board Variables----------------------------------------------//
	CBoard	m_Board;

	//Pawn Movement------------------------------------------------//
	float m_InitXPawn;
	float m_InitZPawn;
	float m_InitXTile;
	float m_InitZTile;
	int m_Destination;
	int m_OtherDest;
	int m_GapTile;
	int m_PawnTile;
	int m_DestinationCheck;

	//game and menu posters-----------------------------------//

	CPoster m_backGrounds[numOfBackGrounds];	
	IDirect3DTexture9* m_backGroundTex[numOfBackGrounds];

	//circuitBoard under gameboard
	CPoster m_cirBoard;
	IDirect3DTexture9*  m_cirBoardTex;

	//posters for number at start
	CPoster m_startBoards[NumberOfPlayers];
	//posters for number at start
	CPoster m_homeBoards[NumberOfPlayers];

	IDirect3DTexture9* m_numberMeshTex[5];

	//used to make poster meshes
	LPDIRECT3DVERTEXBUFFER9 m_buffer;

	//Hand Variables-----------------------------------------------//
	Hand m_Hand[4];
	Deck m_Deck;

	//Controller---------------------------------------------------//
	CXBOXController* Player1;
	//thes help check past state values
	float m_lastTimeX;
	float m_lastTimeY;
	float m_lastButtonState;
	//helps pick dir for camera direction
	bool m_runY;
	bool m_runX;
	//for analog state checking
	leftYState m_leftYSt;
	int m_lastLeftYSt;
	//helps add position change to mouseArrow befor setting pos
	int m_xArrowMove;
	int m_yArrowMove;

	float m_LeftThumbX;
	float m_LeftThumbY;

	float m_SpeedFactorX;
	float m_SpeedFactorY;

	//KeyBoard stuff-------------------------------------------------//
	//this is to hold the keyboards hits
	char m_Buffer[256];
	//this is for a function checks for pressed hits
	bool m_keyCheckBuff[256];	//we may need this still//

	//Players------------------------------------------------------//
	CPlayerController m_PlayerController;
	CPlayer m_Players[NumberOfPlayers];

	//Picking------------------------------------------------------//
	int m_PlayerToBePicked;
	int m_PickObject;
	int m_PlayerPicked;
	int m_CardPicked;
	int m_CardValue;
	int m_PawnPicked;
	int m_TilePicked;
	int m_HomeStretchPicked;
	int m_HomePicked;
	D3DXMATRIX m_matInverse;	//the world view matrix inverse//
	D3DXVECTOR3 m_Origin, m_Direction;	//origin and direction of the ray//

	//Game Loop Logic----------------------------------------------//
	int m_PlayerHasWon;
	int m_PlayerTurn;
	bool redraw;
	//Shader variables and stuff for the highlite mesh
	//these are for making the mesh
	CPoster highLite[3];
	LPD3DXMESH tileFrame;
	IDirect3DTexture9*	tileFrameTex;
	//these are for the shader
	ID3DXEffect* m_pEffect;
	float m_currentTime;
	float m_lastTime;
	float m_shaderIndex;
	float m_shaderPercent;
	
	//----------------------------------------------------------
	//Sound
	CSoundEngine m_soundEng;

	//-------------------------------------------------------------//

	//Functions//

public:
	//DirectX Functions--------------------------------------------//
	//Constructor//
	CDirectXFramework(void);			//Initialize Variables//
	//Destructor//
	~CDirectXFramework(void);			//Null Variables, Call Shutdown//
	//Initialization//
	void InitDX(HWND& hWnd, HINSTANCE& hInst, bool bWindowed);
	//Update//
	void Update(float);					//Update Time is Passed in//
	//Render//
	void Render(float);					//Draw to Screen//
	//Shutdown//
	void Shutdown(void);				//Release Variables//

	//Frames Per Second Functions----------------------------------//
	//Initialize//
	void InitFPS(void);					//Sets up FPS counter//
	//Calculate Frames Per Seond//
	void CalculateFPS(void);			//Fills m_tcFPS with FPS Count//

	//Game Functions-----------------------------------------------//
	bool MouseClick(void);				//If Mouse was lost, returns false//

	//Picking------------------------------------------------------//
	int Pick(int);
	//updates the picking for menu
	void updateMenuPicking(int timer);
	//updates the picking for Play
	void updatePlayPicking(float);

	//KeyBoard---------------------------------------------------//
	//this function just calls aquire and checkKeyBoard
	void UpdateKeyboard(void);
	//this one returns a bool if k key was pressed and released once
	bool checkKeyHits(int k);

	void updatePlayKeyEvents(void);

	//Menu---------------------------------------------------//
	void updateIndex(void);

	//Camera Controls---------------------------------------------------//
	void updateKeyCamMove(float deltaTime);
	void updateGamePadCamMove(float deltaTime);

	//Gamecontols---------------------------------------------------//
	void updatePlayGamePadEvents(void);
	void updateMenuContoller(void);
	void updateArrowByControl(void);
	
	//Board_SetUp------------------------------------------------------//
	void setPawnsOnStart(int num, D3DXVECTOR3 pos);
	//sets up everything for start and home number posters
	//and for now circuitBoard mesh
	void init_posterObjects(void);

	void RunShader(float, float);

	void renderBackground(int num);

	int CheckDestination(void);
	
};